//
//  NextViewController.m
//  重新使用NSTimer
//
//  Created by xiakexing on 2018/5/11.
//  Copyright © 2018年 xiakexing. All rights reserved.
//

#import "NextViewController.h"
#import "ThirdViewController.h"

//定义了一个__weak的self_weak_变量
#define weakifySelf  \
__weak __typeof(&*self)weakSelf = self;

//局域定义了一个__strong的self指针指向self_weak
#define strongifySelf \
__strong __typeof(&*weakSelf)self = weakSelf;

@interface NextViewController ()
@property (nonatomic, strong) NSTimer * timer;  // 当前控制器对timer进行了强引用
@property (nonatomic, weak) NSMutableArray * mutableArr;
@property (nonatomic, weak) NSArray * arr;
@end

@implementation NextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    /*  主线程中执行同步任务造成死锁
    dispatch_sync(dispatch_get_main_queue(), ^{
        [self go];
        NSLog(@"111");
    });
     */
    
    self.view.backgroundColor = [UIColor greenColor];
    // timer对当前控制器进行了强引用
    
    // 直接使用 但会被强引用从而导致无法释放
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(go) userInfo:nil repeats:YES];
    
    // 需要手动添加到RunLoop中才能使用，会被runloop强引用从而导致无法释放
//    self.timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(go) userInfo:nil repeats:YES];
//    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
    
    // 不需要手动添加到RunLoop中，没有无法释放的问题
    weakifySelf
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
//        strongifySelf
//        [self go];
//    }];
    
    // 需要手动添加到RunLoop中，没有无法释放的问题
    self.timer = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        strongifySelf
        [self go];
    }];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
    
    
    self.mutableArr = [NSMutableArray array];
//    [self.arr  addObject:@"1"];
    self.arr = [NSArray array];
    NSLog(@"self.arr:%@", self.mutableArr);
    NSLog(@"self.arr:%@", self.arr);
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn];
    btn.frame = CGRectMake(100, 100, 100, 100);
    [btn setTitle:@"消失" forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor yellowColor];
    [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
}
- (void)clickBtn:(UIButton *)btn{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    [self dismissViewControllerAnimated:YES completion:nil];
    
    ThirdViewController * thirdVC = [[ThirdViewController alloc] init];
    [self presentViewController:thirdVC animated:YES completion:nil];
    
}
- (void)go{
    NSLog(@"self.timer:%@", self.timer);
//    [self. arr addObject:@"2"];
    NSLog(@"self.arr:%@", self.mutableArr);
    NSLog(@"self.arr:%@", self.arr);
    NSLog(@"。。。");
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    [self.timer invalidate];
//    self.timer = nil;
}
- (void)dealloc{
    // 如果timer和当前控制器造成了循环引用，那么dealloc无法被调用
    [self.timer invalidate];
    self.timer = nil;
    NSLog(@"被销毁了....");
    
}


@end
